/**
 * \file PluginManager.cpp
 * \brief PluginManager definition class
 * \author Edouard ROGE
 * \date 16 March 2013
 */

#include "PluginObject.h"
#include "PluginManager.h"

PluginManager::PluginManager()
{
	m_Plugins.Resize(Plugin::Count);
	for(uint32 i=0; i<Plugin::Count; ++i)
		m_Plugins[i]=NULL;
}
	
PluginManager::~PluginManager()
{

}

template <class T>
error PluginManager::RegisterPlugin(Plugin::Type _ePluginType, const String & _strPluginName)
{
	if(m_Plugins[_ePluginType]!=NULL)
	{
		LOG_ERROR("Cannot load %s, a plug-in of the same type is already loaded", _strPluginName);
		return errFailed;
	}

	m_Plugins[_ePluginType] = new PluginObject<T>();

	error e = m_Plugins[_ePluginType]->Load(_strPluginName);

	if(e!=errOK)
	{
		delete m_Plugins[_ePluginType];
		m_Plugins[_ePluginType]=NULL;
		return errFailed;
	}

	return errOK;
}
	
error PluginManager::UnregisterPlugin(Plugin::Type _ePluginType)
{
	if(m_Plugins[_ePluginType]==NULL)
	{
		LOG_ERROR("Cannot unload plug-in");
		return errFailed;
	}

	m_Plugins[_ePluginType]->Unload();
	delete m_Plugins[_ePluginType];
	m_Plugins[_ePluginType]=NULL;

	return errOK;
}

Plugin * PluginManager::GetPlugin(Plugin::Type _ePluginType) const
{
	return m_Plugins[_ePluginType];
}
	
PluginInterface * PluginManager::GetPluginInterface(Plugin::Type _ePluginType) const
{
	return m_Plugins[_ePluginType]-> GetInterface();
}
